package com.edu.chat.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.buservice.util.model.TenantUtil;
import com.edu.chat.facade.model.dto.HistoryMsgDTO;
import com.edu.chat.facade.model.dto.SendMessageDTO;
import com.edu.chat.web.controller.vo.QueryMsgVO;
import com.edu.chat.web.covert.MessageCovert;
import com.edu.chat.web.mapper.CsMessageMapper;
import com.edu.chat.web.model.CsMessage;
import com.edu.chat.web.service.CsMessageService;
import com.edu.robot.model.enums.ResultCodeEnum;
import com.edu.robot.model.error.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author 86177
* @description 针对表【cs_message(历史消息表)】的数据库操作Service实现
* @createDate 2025-03-23 18:23:55
*/
@Service
@Slf4j
public class CsMessageServiceImpl extends ServiceImpl<CsMessageMapper, CsMessage>
    implements CsMessageService{

	@Override
	public List<HistoryMsgDTO> queryMsgList(String sessionId) {

		if (StringUtils.isBlank(sessionId)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"sessionId不能为空");
		}

		LambdaQueryWrapper<CsMessage> queryWrapper=new LambdaQueryWrapper<>();

		queryWrapper.eq(CsMessage::getSessionId,sessionId);
		queryWrapper.eq(CsMessage::getDeleted,0);
		queryWrapper.orderByDesc(CsMessage::getCreatedAt);

		//获取最近8个消息
		queryWrapper.last("limit 8");

		List<CsMessage> list = list(queryWrapper);

		if (list.isEmpty()){
			return List.of();
		}

		List<HistoryMsgDTO> collect = list.stream().map(MessageCovert::convert).collect(Collectors.toList());

		return collect;
	}

	@Override
	public void insertMsg(SendMessageDTO sendMessageDTO) {
		log.info("CsMessageServiceImpl insertMsg:{}",sendMessageDTO);

		if (sendMessageDTO==null){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"sendMessageDTO不能为空");
		}

		CsMessage convert = MessageCovert.convert(sendMessageDTO);

		this.save(convert);

	}

	@Override
	public List<HistoryMsgDTO> queryMsgListByScroll(QueryMsgVO queryMsgVO) {

		if (queryMsgVO==null){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"queryMsgVO不能为空");
		}

		//TODO 滚动消息，现在先一次查询完·

		if (StringUtils.isBlank(queryMsgVO.getSessionId())){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"sessionId不能为空");
		}

		LambdaQueryWrapper<CsMessage> queryWrapper=new LambdaQueryWrapper<>();

		queryWrapper.eq(CsMessage::getSessionId,queryMsgVO.getSessionId());
		queryWrapper.eq(CsMessage::getDeleted,0);

		String tenantId = Optional.ofNullable(queryMsgVO.getTenantId()).orElse(TenantUtil.getTenantId());
		TenantUtil.setCurrentTenantId(tenantId);

		//获取最近8个消息
		queryWrapper.last("limit 8");

		List<CsMessage> list = list(queryWrapper);

		if (list.isEmpty()){
			return List.of();
		}

		return list.stream().map(MessageCovert::convert).collect(Collectors.toList());

	}

}




